import 'package:cached_network_image/cached_network_image.dart';
import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:www.znpigai.teacher/global_store/state.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(MineState state, Dispatch dispatch, ViewService viewService) {
  bool isMember() {
    return state.userInfo != null &&
        state.userInfo.isNotEmpty &&
        state.userInfo.containsKey('accountInfo') &&
        (state.userInfo['accountInfo']['validRechargeUser'] ||
            state.userInfo['accountInfo']['validBillingCardUser']);
  }

  String memberString() {
    if (isMember()) {
      String time = state.userInfo['accountInfo']['rechargeExpiryTime'];
      if (time == null || time.isEmpty) {
        time = state.userInfo['accountInfo']['billingCardExpiryDate'] ?? '';
      }
      return "会员到期时间：$time";
    } else {
      return '您还不是会员哦！';
    }
  }

  return SingleChildScrollView(
    child: Stack(
      children: <Widget>[
        Positioned(
          left: 0,
          right: 0,
          top: 0,
          height: 120,
          child: Container(
            color: Colors.blue,
          ),
        ),
        Column(
          children: <Widget>[
            SizedBox(height: 36),
            Center(
                child: Text(
              '我的',
              style: TextStyle(color: Colors.white, fontSize: 20),
            )),
            SizedBox(height: 10),
            Card(
              margin: EdgeInsets.symmetric(horizontal: 10),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(5.0),
              ),
              child: Column(
                children: <Widget>[
                  SizedBox(height: 20),
                  Row(
                    children: <Widget>[
                      SizedBox(width: 10),
                      FlatButton(
                        child: Stack(
                          alignment: Alignment.center, //指定未定位或部分定位widget的对齐方式
                          children: <Widget>[
                            CachedNetworkImage(
                              width: 60,
                              height: 60,
                              placeholder: (context, url) =>
                                  CircularProgressIndicator(),
                              errorWidget: (context, url, error) =>
                                  Image.asset("assets/images/user.png"),
                              imageUrl: (state.userInfo != null &&
                                      state.userInfo.isNotEmpty)
                                  ? GlobalState.baseUrl(
                                      state != null && state.userInfo != null
                                          ? state.userInfo['headImage']
                                          : '')
                                  : '',
                              fit: BoxFit.cover,
                            ),
                            Positioned(
                              right: 0,
                              bottom: 0,
                              width: 20,
                              height: 20,
                              child: Opacity(
                                opacity: isMember() ? 1.0 : 0.0,
                                child: Image.asset("assets/images/member.png"),
                              ),
                            ),
                          ],
                        ),
                        onPressed: () => dispatch(MineActionCreator.onAvatar()),
                      ),
                      Expanded(
                        flex: 3,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Text((state.userInfo != null &&
                                    state.userInfo.isNotEmpty)
                                ? state.userInfo['name'] ?? ''
                                : ''),
                            Text(memberString()),
                          ],
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 20),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      Column(children: <Widget>[
                        Text((state.userInfo != null &&
                                state.userInfo.isNotEmpty)
                            ? state.userInfo['hwTotal']
                            : ''),
                        Text('累计批改'),
                      ]),
                      Column(children: <Widget>[
                        Text((state.userInfo != null &&
                                state.userInfo.isNotEmpty)
                            ? state.userInfo['hwTotalRemark']
                            : ''),
                        Text('超过'),
                      ]),
                      Column(children: <Widget>[
                        Text((state.userInfo != null &&
                                state.userInfo.isNotEmpty)
                            ? state.userInfo['logincount']
                            : ''),
                        Text('登录次数'),
                      ]),
                    ],
                  ),
                  SizedBox(height: 20),
                  Container(
                    height: 5,
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.only(
                          bottomLeft: Radius.circular(5),
                          bottomRight: Radius.circular(5)),
                    ),
                  ),
                ],
              ),
            ),
            SizedBox(height: 10),
            Container(
              height: 54.0,
              child: ListTile(
                title: new Text('班级管理'),
                leading: Container(
                  width: 32.0,
                  height: 32.0,
                  padding: EdgeInsets.all(4.0),
                  child: ImageIcon(
                    AssetImage("assets/images/mine_item_class_manage.png"),
                    color: Colors.blue,
                    size: 24.0,
                  ),
                ),
                trailing: ImageIcon(
                  AssetImage("assets/images/right.png"),
                  size: 24.0,
                ),
                onTap: () => dispatch(MineActionCreator.onClass()),
              ),
            ),
            new Divider(
              height: 1,
              indent: 60,
            ),
            Container(
              height: 54.0,
              child: ListTile(
                title: new Text('我的消息'),
                leading: Container(
                  width: 32.0,
                  height: 32.0,
                  padding: EdgeInsets.all(4.0),
                  child: ImageIcon(
                    AssetImage("assets/images/message_white.png"),
                    color: Colors.blue,
                    size: 24.0,
                  ),
                ),
                trailing: ImageIcon(
                  AssetImage("assets/images/right.png"),
                  size: 24.0,
                ),
                onTap: () => dispatch(MineActionCreator.onMessage()),
              ),
            ),
            new Divider(
              height: 1,
              indent: 60,
            ),
//            Container(
//              height: 54.0,
//              child: ListTile(
//                title: new Text('VIP会员'),
//                leading: Container(
//                  width: 32.0,
//                  height: 32.0,
//                  padding: EdgeInsets.all(4.0),
//                  child: ImageIcon(
//                    AssetImage("assets/images/mine_item_vip_icon.png"),
//                    color: Colors.blue,
//                    size: 24.0,
//                  ),
//                ),
//                trailing: ImageIcon(
//                  AssetImage("assets/images/right.png"),
//                  size: 24.0,
//                ),
//                onTap: () => dispatch(MineActionCreator.onPay()),
//              ),
//            ),
//            new Divider(
//              height: 1,
//              indent: 60,
//            ),
//            Container(
//              height: 54.0,
//              child: ListTile(
//                title: new Text('我的订单'),
//                leading: Container(
//                  width: 32.0,
//                  height: 32.0,
//                  padding: EdgeInsets.all(4.0),
//                  child: ImageIcon(
//                    AssetImage("assets/images/mine_item_minedingdan.png"),
//                    color: Colors.blue,
//                    size: 24.0,
//                  ),
//                ),
//                trailing: ImageIcon(
//                  AssetImage("assets/images/right.png"),
//                  size: 24.0,
//                ),
//                onTap: () => dispatch(MineActionCreator.onPay()),
//              ),
//            ),
//            new Divider(
//              height: 1,
//              indent: 60,
//            ),
            Container(
              height: 54.0,
              child: ListTile(
                title: Text('个人信息'),
                leading: Container(
                  width: 32.0,
                  height: 32.0,
                  padding: EdgeInsets.all(4.0),
                  child: ImageIcon(
                    AssetImage("assets/images/user_white.png"),
                    color: Colors.blue,
                    size: 24.0,
                  ),
                ),
                trailing: ImageIcon(
                  AssetImage("assets/images/right.png"),
                  size: 24.0,
                ),
                onTap: () => dispatch(MineActionCreator.onProfile()),
              ),
            ),
            new Divider(
              height: 1,
              indent: 60,
            ),
            Container(
              height: 54.0,
              child: ListTile(
                title: Text('意见反馈'),
                leading: Container(
                  width: 32.0,
                  height: 32.0,
                  padding: EdgeInsets.all(4.0),
                  child: ImageIcon(
                    AssetImage("assets/images/mine_feedback.png"),
                    color: Colors.blue,
                    size: 24.0,
                  ),
                ),
                trailing: ImageIcon(
                  AssetImage("assets/images/right.png"),
                  size: 24.0,
                ),
                onTap: () => dispatch(MineActionCreator.onFeedback()),
              ),
            ),
            new Divider(
              height: 1,
              indent: 60,
            ),
            Container(
              height: 54.0,
              child: ListTile(
                title: Text('关于IN课堂'),
                leading: Container(
                  width: 32.0,
                  height: 32.0,
                  padding: EdgeInsets.all(4.0),
                  child: ImageIcon(
                    AssetImage("assets/images/mine_item_about.png"),
                    color: Colors.blue,
                    size: 24.0,
                  ),
                ),
                trailing: ImageIcon(
                  AssetImage("assets/images/right.png"),
                  size: 24.0,
                ),
                onTap: () => dispatch(MineActionCreator.onAbout()),
              ),
            ),
            SizedBox(height: 10),
            Container(
                child: RaisedButton(
              onPressed: () => dispatch(MineActionCreator.onLogout()),
              colorBrightness: Brightness.dark,
              child: Text("退出登录", style: new TextStyle(color: Colors.black)),
            )),
            SizedBox(height: 50),
          ],
        ),
      ],
    ),
  );
}
